<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head th:include="public/page_css::page_css ('提取PO')"/>

<body>
<div class="layui-fluid larry-wrapper">
    <div class="layui-card">
        <div class="layui-row layui-col-space30">
            <div class="layui-col-xs24">
                <form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm2">
                    <div class="layui-inline">
                        <label class="layui-form-label">PO编号</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="bh" name="bh" placeholder="PO编号"/>
                        </div>
                    </div>
                    <div class="layui-inline inline-search-btn">
                        <a class="layui-btn layui-btn-normal search_btn" lay-event="searchEvent"
                           style="text-decoration:none"
                           data-type="reload">查询</a>
                    </div>
                    <div class="layui-inline inline-reset-btn">
                        <button type="reset" class="layui-btn layui-btn-normal" style="background-color: #40AFFE">重置
                        </button>
                    </div>
                </form>
                <section class="panel panel-padding layui-card-body">
                    <div class="pro">
                        <table id="table2" class="table text-nowrap"></table>
                    </div>
                </section>
            </div>
        </div>
    </div>
</div>
<!--下面用来处理PO新增预期到货通知单-->
<div id="addPoCz" style="display: none">
    <div class="layui-fluid larry-wrapper">
        <div class="layui-card">
            <div class="layui-row layui-col-space30">
                <div class="layui-col-xs24">
                    <form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
                        <div class="layui-inline">
                            <label>ASN编号</label>
                            <div class="layui-form-inline">
                                <input type="text" class="layui-input" id="asnbh" name="asnbh" readonly="readonly"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label>预计到货时间</label>
                            <div class="layui-form-inline">
                                <input type="text" class="layui-input" name="stime" id="stime"
                                       placeholder="yyyy-MM-dd HH:mm:ss"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label>优先级</label>
                            <div class="layui-form-inline" style="width: 100px">
                                <select id="yxj" name="yxj" lay-filter="aihao" disabled>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label>ASN类型</label>
                            <div class="layui-form-inline">
                                <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="selectId"
                                        name="telephone" select2 style="width: 193px" disabled="disabled"></select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label>供应商编号</label>
                            <div class="layui-form-inline">
                                <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="gysId"
                                        name="telephone1" select2 style="width: 193px" disabled="disabled"></select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label>审核状态</label>
                            <div class="layui-form-inline">
                                <input type="text" class="layui-input" id="shzt" name="shzt" readonly="readonly" style="width: 80px"/>
                            </div>
                        </div>
                    </form>
                    <!--列表-->
                    <section class="panel panel-padding layui-card-body">
                        <div class="pro">
                            <table id="table5" class="table text-nowrap"></table>
                        </div>
                    </section>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<!--分配-->
<div id="editfunction" style="display: none">
    <form class="layui-form layui-card-header  layui-card-header-auto" id="form2">
        <div class="layui-inline">
            <label>货品编号</label>
            <div class="layui-form-inline">
                <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="goodsId" disabled="disabled"
                        name="hpbh" select2 style="width: 193px"></select>
            </div>
        </div>
        <div class="layui-inline">
            <label>货品名称</label>
            <div class="layui-form-inline">
                <input type="text" id="hpmc" name="hpmc" placeholder="货品名称" autocomplete="off" class="layui-input "
                       readonly="readonly"/>
            </div>
        </div>
        <div class="layui-inline">
            <label>规格</label>
            <div class="layui-form-inline">
                <input type="text" id="gg" name="gg" placeholder="规格" readonly="readonly" autocomplete="off"
                       class="layui-input "/>
            </div>
        </div>
        <div class="layui-inline">
            <label>入库单位</label>
            <div class="layui-form-inline">
                <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="rkdw" disabled="disabled"
                        name="rkdw" select2 style="width: 180px"></select>
            </div>
        </div>
        <br/>
        <div class="layui-inline">
            <label>货品批号</label>
            <div class="layui-form-inline">
                <input type="text" id="hpph" name="hpph" placeholder="货品批号" lay-verify="required" autocomplete="off"
                       class="layui-input "/>
            </div>
        </div>
        <div class="layui-inline">
            <label>生产日期</label>
            <div class="layui-form-inline">
                <input type="text" class="layui-input" name="scrq" id="scrq"
                       placeholder="yyyy-MM-dd HH:mm:ss"/>
            </div>
        </div>
        <div class="layui-inline">
            <label>失效日期</label>
            <div class="layui-form-inline">
                <input type="text" class="layui-input" name="sxrq" id="sxrq"
                       placeholder="yyyy-MM-dd HH:mm:ss" disabled="disabled"/>
            </div>
        </div>
        <div class="layui-inline">
            <label>主单位（个）</label>
            <div class="layui-form-inline">
                <input type="text" id="yqsl" name="yqsl" placeholder="主单位数量" lay-verify="required" disabled="true"
                       autocomplete="off" class="layui-input "/>
            </div>
        </div>
        <br/>
        <div class="layui-inline">
            <label>体积（cm³）</label>
            <div class="layui-form-inline">
                <input type="text" id="tj" name="tj" placeholder="体积" readonly="readonly" autocomplete="off"
                       class="layui-input "/>
                <span hidden id="tjS"></span>
            </div>
        </div>
        <div class="layui-inline">
            <label>净重（kg）</label>
            <div class="layui-form-inline">
                <input type="text" id="jz" name="jz" placeholder="净重" readonly="readonly" autocomplete="off"
                       class="layui-input "/>
                <span hidden id="jzS"></span>
            </div>
        </div>
        <div class="layui-inline">
            <label>毛重（kg）</label>
            <div class="layui-form-inline">
                <input type="text" id="zl" name="zl" placeholder="毛重" readonly="readonly" autocomplete="off"
                       class="layui-input "/>
                <span hidden id="zlS"></span>
            </div>
        </div>
        <div class="layui-inline">
            <label>箱单位（xx箱xx件）</label>
            <div class="layui-form-inline">
                <input type="text" id="xdwsl" name="xdwsl" placeholder="箱单位数量" lay-verify="required" disabled="true"
                       autocomplete="off" class="layui-input "/>
            </div>
        </div>
        <br/>
        <div class="layui-inline">
            <label>金额(元)</label>
            <div class="layui-form-inline">
                <input type="text" id="qian" name="qian" placeholder="金额" readonly="readonly" autocomplete="off"
                       class="layui-input "/>
            </div>
        </div>
        <div class="layui-inline">
            <label>总金额(元)</label>
            <div class="layui-form-inline">
                <input type="text" id="je" name="je" placeholder="总金额" lay-verify="required" th:value="0"
                       autocomplete="off" class="layui-input "/>
            </div>
        </div>
        <div class="layui-inline">
            <label>备注</label>
            <div class="layui-form-inline">
                <input type="text" id="bz" name="bz" placeholder="备注" lay-verify="required" autocomplete="off"
                       class="layui-input " style="width: 380px;"/>
            </div>
        </div>
    </form>
</div>
</body>
<div th:include="public/public_js::public_js"></div>

<script th:inline="javascript">
    $(function () {
    })

</script>
<script>
    var tables;
    var UnitNum = 1;
    var sscgdid;
    var locking;
    var rkdwId;//声明一个全局变量的入库单位id
    var zname;//声明主单位名称
    layui.use(['myform', 'zzpUtils', 'form', 'jquery', 'laydate'], function () {
        window.operateEvent = {
            "click #btn_tq": function (e, value, row, index) {//提取
                btn_tq(row.id);
            }
        }
        window.operateEvent1 = {

            "click #btn_fp": function (e, value, row, index) {//分配
                edit(row);
            },
            "click #btn_delsc": function (e, value, row, index) {//删除
                del_fp(row);
            }
        }
        var form = layui.form,
            zzpUtils = layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate;

        laydate.render({
            elem: '#stime'
            , theme: '#20a0ff'
            , range: '~'//练起来
        });
        laydate.render({
            elem: '#scrq'
            , theme: '#20a0ff'
        });
        laydate.render({
            elem: '#sxrq'
            , theme: '#20a0ff'
        })

        //提取
        function btn_tq(id) {
            layer.open({
                type: 2,
                title: "选择提取条数",
                content: "/zxsswms/yqdh/getOnePO?id=" + id,
                area: ['95%', '95%'],
                btn: ['保存', '取消'],
                btn1: function (index, layero) {//获取子级数据
                    tables.bootstrapTable('removeAll');
                    var ids = [];
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    var row = iframeWin.sureChoose();
                    var array = JSON.stringify(row);
                    var jsObject = JSON.parse(array);    //转换为json对象
                    sscgdid = jsObject[0].sscgdid;
                    console.log(jsObject[0].rows);
                    if (jsObject[0].rows.length == 0) {
                        layer.msg("您还未选择，请您至少选择一个！");
                    } else {
                        layer.open({
                            type: 1,
                            title: "新增预期到货通知",
                            content: $("#addPoCz"),
                            area: ['95%', '95%'],
                            btn: ['保存', '取消'],
                            success: function (layero, index) {
                                //回显显示
                                zzpUtils.action({
                                    url: "/wmsEstimatedArrivalOrder/getInfoByNew",
                                    data: JSON.stringify({id: sscgdid}),
                                    success: function (data) {
                                        //zzpUtils.loadJsonDataToForm(data);
                                        $('#asnbh').val(data.asnbh);
                                        $('#yxj').val(data.yxj);
                                        $('#selectId').html('<option value="' + data.asnlx + '">' + data.lxmc + '</option>').trigger("change");
                                        $('#gysId').html('<option value="' + data.gysid + '">' + data.gysbh + " " + data.gysmc + '</option>').trigger("change");
                                        $('#stime').val(data.yjdhsjks.substring(0, 10) + ' ~ ' + data.yjdhsjjs.substring(0, 10));
                                        locking=data.locking;
                                        if (data.shzt == 0) {
                                            $('#shzt').val('不自动');
                                        }
                                    }, error: function (xhr, textStatus) {
                                        console.log(xhr)
                                    }
                                });
                                //预期到货明细添加数据
                                for (var i = 0; i < jsObject[0].rows.length; i++) {
                                    var num = parseInt(Math.random() * 100) + 1;
                                    var pass = {};
                                    pass.idex = num;
                                    pass.hpid = jsObject[0].rows[i].hpid;
                                    pass.bh = jsObject[0].rows[i].hpbh;
                                    pass.zdwid = jsObject[0].rows[i].zdwid;
                                    pass.ms = jsObject[0].rows[i].bzmc;
                                    pass.hpmc = jsObject[0].rows[i].hpmc;
                                    pass.yqsl = jsObject[0].rows[i].yqsl;
                                    pass.xdwsl = jsObject[0].rows[i].xdwsl;
                                    pass.je = jsObject[0].rows[i].je;
                                    pass.dycgmxid = jsObject[0].rows[i].dycgmxid;
                                    pass.fpzt = 0;
                                    tables.bootstrapTable('insertRow', {index: num, row: pass});
                                }
                            },
                            btn1: function (index, layero) {
                                //开始执行保存
                                var lis = tables.bootstrapTable('getData');
                                var asnbh = $("#asnbh").val();//ASN编号
                                var stime = $("#stime").val();//预计到货时间
                                var yxj = $("#yxj").val();//优先级
                                var selectId = $("#selectId ").select2("val");//ASN类型
                                var gysId = $("#gysId ").select2("val");//供应商
                                var shzt = $("#shzt").val();//审核状态
                                //声明一个集合，来存明细数据哟哟哟
                                var list = {};
                                var array2 = [];
                                var number6=0;
                                for (var i = 0; i < lis.length; i++) {
                                    if (lis[i].fpzt == 1) {
                                        list={};
                                        list.bh = lis[i].bh;
                                        list.bzmc = lis[i].ms;
                                        list.dycgmxid = lis[i].dycgmxid;
                                        list.gg = lis[i].gg;
                                        list.hpbh = lis[i].bh;
                                        list.hpid = lis[i].hpid;
                                        list.hpmc = lis[i].hpmc;
                                        list.hppc = lis[i].hppc;
                                        list.jz = lis[i].jz;
                                        list.mc = lis[i].hpmc;
                                        list.ms = lis[i].ms;
                                        list.scrq = lis[i].scrq;
                                        list.sxrq = lis[i].sxrq;
                                        list.tj = lis[i].tj;
                                        list.xdwsl = lis[i].xdwsl;
                                        list.yqsl = lis[i].yqsl;
                                        list.zdwid = lis[i].zdwid;
                                        //list.zdwsl=lis[i].zdwsl;
                                        list.zl = lis[i].zl;
                                        array2.push(list);
                                    }else{
                                        if(lis[i].yqsl>0){//如果主单位数量大于0，即还没有分配完
                                            number6++;
                                        }
                                    }
                                }
                                //asnbh  asnlx ckmc  locking:2 dycgdid gysid gysmc mpdyzt yjdhsjjs yjdhsjks yxj zdr
                                var arr = stime.split("~");
                                var pass1 = {};
                                pass1.asnbh = asnbh;
                                pass1.yxj = yxj;
                                pass1.asnlx = selectId;
                                pass1.gysid = gysId;
                                pass1.locking = locking;
                                pass1.gysmc = $("#gysId").select2("data")[0].text;
                                pass1.yjdhsjks = arr[0];
                                pass1.yjdhsjjs = arr[1];
                                pass1.detailed = array2;
                                pass1.dycgdid=sscgdid;
                                if (number6 <=0) {
                                    zzpUtils.confirm("是否确定新增", function (index) {
                                        zzpUtils.action({
                                            url: "/wmsEstimatedArrivalOrder/add",
                                            data: JSON.stringify(pass1),
                                            success: function (data) {
                                                setTimeout(function () {
                                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                    parent.layer.close(index); //再执行关闭
                                                    if(data.type=='success'){
                                                        top.layer.msg(data.title);
                                                    }else{
                                                        top.layer.msg(data.message);
                                                    }
                                                }, 500);
                                                zzpUtils.refreshTable("#table");
                                            }, error: function (xhr, textStatus) {
                                                console.log(textStatus);
                                                console.log(xhr)
                                            }
                                        })
                                    })
                                    return false;
                                } else {
                                    layer.msg("明细中 存在未分配完全的,无法保存！");
                                }
                            },
                            btn2: function (index, layero) {
                                //取消将数据全部删除
                                tables.bootstrapTable('removeAll');
                                layer.close(index);
                            }
                        });
                    }
                    // layer.close(index);
                },
                btn2: function (index, layero) {
                    layer.close(index);
                }
            });
        }

        //提取PO中table
        var $table = zzpUtils.table({
            elem: '#table2',
            isApi: true,
            pagination: true,
            url: '/wmsEstimatedArrivalOrder/getPoList',
            columns: [{
                field: 'ckmc',
                title: '仓库'
            }, {
                field: 'gysmc',
                title: '供应商'
            }, {
                field: 'bh',
                title: 'PO编号'
            }, {
                field: 'caozuo',
                title: '可用操作',
                width: 200,
                events: operateEvent,
                formatter: operateFormatter1
            }],
            queryParams: function () {//查询条件
                return zzpUtils.getFormJson($("#searchForm2"));
            },
        });
        //查询
        $(".inline-search-btn").on('click', function () {
            zzpUtils.refreshTable("#table2");
        })

        /*表格右侧按钮*/
        function operateFormatter1(value, row, index) {
            return [
                '<a class="layui-btn layui-btn-xs layui-btn-normal" style="text-decoration: none" id="btn_tq">提取</a>'
            ].join('')
        }

        /*=====================================以下是关于Po新增预期到货通知单==================================================================*/
        /*select2单选
       * ASN类型
       * */
        zzpUtils.select2({
            elem: "#selectId",
            url: "/wmsPurchaseOrder/getSelectLx",
            minimumInputLength: 0
        });
        /*
        * 供应商编号
        * */
        zzpUtils.select2({
            elem: "#gysId",
            url: "/wmsPurchaseOrder/getGysmc",
            minimumInputLength: 0
        });
        tables = zzpUtils.table({
            elem: '#table5',
            isApi: true,
            uniqueId: "idex",
            pagination: true,
            showColumns: false, //是否显示所有的列
            //url: '',
            columns: [/*{
                field: '',
                checkbox: true
            },*/ {
                field: 'idex',
                title: '暂用id',
                visible: false
            }, {
                field: 'idex1',
                title: '暂用父id',
                visible: false
            }, {
                field: 'dycgmxid',
                title: '对应采购明细ID',
                visible: false
            }, {
                field: 'qian',
                title: '钱',
                visible: false
            }, {
                field: 'fpzt',
                title: '分配状态',
                visible: false
            }, {
                field: 'gysid',
                title: '供应商id',
                visible: false
            }, {
                field: 'hpid',
                title: '货品id',
                visible: false
            }, {
                field: 'zdwid',
                title: '主单位id',
                visible: false,
            }, {
                field: 'bh',
                title: '货品编号'
            }, {
                field: 'hpmc',
                title: '货品名称'
            }, {
                field: 'gg',
                title: '规格'
            }, {
                field: 'nc',
                title: 'NC编码'
            }, {
                field: 'ms',
                title: '主单位'
            }, {
                field: 'hppc',
                title: '货品批次'
            }, {
                field: 'scrq',
                title: '生产日期'
            }, {
                field: 'yqsl',
                title: '主单位数量'
            }, {
                field: 'xdwsl',
                title: '箱单位数量'
            }, {
                field: 'sxrq',
                title: '失效日期'
            }, {
                field: 'pssl',
                title: '破损数量'
            }, {
                field: 'jssl',
                title: '拒收数量'
            }, {
                field: 'bz',
                title: '备注'
            }, {
                field: 'tj',
                title: '体积'
            }, {
                field: 'jz',
                title: '净重'
            }, {
                field: 'zl',
                title: '毛重'
            }, {
                field: 'je',
                title: '金额'
            }, {
                field: 'caozuo',
                title: '操作',
                events: operateEvent1,
                formatter: operateFormatter2
            }],
            detailFormatter: function (index, row) {
                return "这里就没有确定过11";
            }
        });

        /*表格右侧按钮*/
        function operateFormatter2(value, row, index) {

            if (row.fpzt == 0 && row.yqsl > 0) {//分配状态 0 未分配 1已分配
                return [
                    '<a class="layui-btn layui-btn-xs layui-btn-normal" style="text-decoration: none" id="btn_fp">分配</a>'
                ].join('')
            } else if (row.fpzt == 1) {
                return [
                    '<a class="layui-btn layui-btn-xs layui-btn-normal" style="text-decoration: none" id="btn_delsc">删除</a>'
                ].join('')
            }

        }

        //分配
        function edit(row) {
            layer.open({
                type: 1,
                title: "分配",
                content: $("#editfunction"),
                area: ['62%', '70%'],
                success: function (layero, index) {
                    $('#goodsId').html('<option value="' + row.hpid + '">' + row.bh + '</option>').trigger("change");
                    $('#rkdw').html('<option value="' + row.zdwid + '">' + row.ms + '</option>').trigger("change");
                    $('#scrq').val(getNowFormatDate());
                    $('#hpph').val(getNowFormatDate1());
                    $('#yqsl').val(row.yqsl);
                    $('#xdwsl').val(row.xdwsl);
                    $('#je').val(row.je);
                    //通过货品id查询信息
                    $.ajax({
                        url: "/zxsswms/wmsEstimatedArrivalOrder/getOneGood",       //请求地址
                        type: "POST",            //请求方式
                        data: {id: row.hpid},    //请求参数
                        success: function (data) {
                            $("#hpmc").val(data.goodsName);
                            $("#gg").val(data.gg);
                            $("#sxrq").val(transferCouponValueTime($('#scrq').val(), data.yxq));//失效日期
                            //获取包装信息
                            var warehouseunit = wareHouseUnit(data.bzid);
                            for (var i = 0; i < warehouseunit.length; i++) {
                                if (warehouseunit[i].bzlx == '0') {
                                    rkdwId = warehouseunit[i].id;
                                    zname = warehouseunit[i].ms;
                                }
                                if (warehouseunit[i].id == row.zdwid) {
                                    $("#tj").val(warehouseunit[i].tj);
                                    $("#jz").val(warehouseunit[i].jz);
                                    $("#zl").val(warehouseunit[i].zl);
                                    $("#qian").val(warehouseunit[i].je);
                                    $("#tjS").val(warehouseunit[i].tj);
                                    $("#jzS").val(warehouseunit[i].jz);
                                    $("#zlS").val(warehouseunit[i].zl);
                                    // 主单位数量
                                    UnitNum = warehouseunit[i].zdwsl;
                                    if (warehouseunit[i].bzlx == '0') {//主单位

                                        $('#yqsl').attr("disabled", false);
                                        $('#xdwsl').attr("disabled", true);

                                    } else {//箱单位

                                        $('#yqsl').attr("disabled", true);
                                        $('#xdwsl').attr("disabled", false);
                                    }
                                }
                            }
                        }
                    });

                    //select2获取入库单位xing
                    $('#rkdw').select2({
                        ajax: {
                            url: '/zxsswms/wmsEstimatedArrivalOrder/getRkdw',
                            dataType: 'json',
                            type: 'POST',
                            contentType: "application/json",
                            data: function (params) {
                                var query = {
                                    hpid: row.hpid
                                };
                                return JSON.stringify(query);
                            },
                            processResults: function (data) {
                                var arr = new Array();
                                if (data) {
                                    for (var i = 0; i < data.length; i++) {
                                        arr.push({id: data[i].id, text: data[i].ms});
                                    }
                                }
                                return {
                                    results: arr
                                };
                            },
                            cache: true
                        },
                        language: "zh-CN",
                        placeholder: '请选择入库单位',
                        allowClear: true
                    });
                },
                btn: ['保存', '取消'],
                btn1: function (index, layero) {
                    //[分配]保存前判断
                    var num = parseInt(Math.random() * 100) + 1;
                    var rows = tables.bootstrapTable('getData');
                    var hpid = $("#goodsId").select2("val");
                    var zdwid =rkdwId;
                    var yqsl = $('#yqsl').val();
                    var hpbh = $("#goodsId").select2("data")[0].text;
                    var hppc = $('#hpph').val();
                    var time = $('#scrq').val();
                    var number = 0;

                    if (rows.length > 0) {
                        for (var i = 0; i < rows.length; i++) {
                            if (row.bh != rows[i].bh || row.hppc != rows[i].hppc || row.scrq != rows[i].scrq) {
                                if (rows[i].bh == hpbh && rows[i].hppc == hppc && rows[i].scrq == time) {
                                    number++;
                                }
                            }
                        }
                    }
                    if (number > 0) {
                        layer.msg("发现 完全相同的 货品编号" + hpbh + " 货品批次" + hppc + " 生产日期" + time + " 已经存在！");
                    } else if (Number(yqsl) > Number(row.yqsl)) {
                        layer.msg("发现 预期数量超出限制！ 最大预期数量为：" + row.yqsl + " 您要分配的数量为: " + yqsl);
                    } else {
                        var pass = {};
                        pass.idex = num;
                        pass.hpid = hpid;
                        pass.bh = hpbh;
                        pass.zdwid = zdwid;
                        pass.ms = zname;
                        pass.hpmc = $('#hpmc').val();
                        pass.gg = $('#gg').val();
                        pass.hppc = hppc;
                        pass.scrq = time;
                        pass.yqsl = yqsl;
                        pass.xdwsl = $('#xdwsl').val();
                        pass.sxrq = $('#sxrq').val();
                        pass.bz = $('#bz').val();
                        pass.tj = $('#tj').val();
                        pass.jz = $('#jz').val();
                        pass.zl = $('#zl').val();
                        pass.je = $('#je').val();
                        pass.qian = $('#qian').val();
                        pass.pssl = 0;
                        pass.jssl = 0;
                        pass.fpzt = 1;
                        pass.idex1 = row.idex;
                        pass.dycgmxid = row.dycgmxid;
                        tables.bootstrapTable('insertRow', {index: num, row: pass});
                        //保存后修改原数量
                        var pass1 = {};
                        var num = row.yqsl - yqsl;
                        pass1.yqsl = num;
                        tables.bootstrapTable('updateByUniqueId', {id: row.idex, row: pass1});
                        layer.close(index);
                    }
                }
                , btn2: function (index, layero) {
                    layer.close(index);
                }
            })

        }

        //分配删除
        function del_fp(sow) {
            var ids = [];
            ids.push(sow.idex);
            tables.bootstrapTable('remove', {field: 'idex', values: ids});
            var rows = tables.bootstrapTable('getRowByUniqueId', sow.idex1);
            var pass2 = {};
            pass2.yqsl = Number(rows.yqsl) + Number(sow.yqsl);
            tables.bootstrapTable('updateByUniqueId', {id: rows.idex, row: pass2});

        }

        //select2查询商品 xxx
        $('#goodsId').select2({
            ajax: {
                url: '/zxsswms/wmsPurchaseOrder/getGoods',
                dataType: 'json',
                type: 'POST',
                contentType: "application/json",
                data: function (params) {
                    var query = {
                        bh: params.term,
                        gysid: $("#gysId").select2("val")
                    };
                    return JSON.stringify(query);
                },
                processResults: function (data) {
                    var arr = new Array();
                    if (data) {
                        for (var i = 0; i < data.length; i++) {
                            arr.push({id: data[i].goodsId, text: data[i].goodsBh});
                        }
                    }
                    return {
                        results: arr
                    };
                },
                cache: true
            },
            language: "zh-CN",
            placeholder: '请选择货品编号',
            allowClear: true
        });
        // 查询入库单位
        function wareHouseUnit(id) {
            var DATA;
            $.ajax({
                url: '/zxsswms/wmsPurchaseOrder/packDetailList/' + id,
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json',
                async: false,
                success: function (data) {
                    DATA = data;
                }
            })
            return DATA;
        }

        // 监听主单位数量
        $('#yqsl').on('input propertychange', function (data) {
            var value = data.currentTarget.value;
            var len, lenfloat;
            var volumeS = parseFloat($('#jzS').val());//净重
            var suttleS = parseFloat($('#zlS').val());//毛重
            var grossWeightS = parseFloat($('#tjS').val());//体积
            var money = parseFloat($('#qian').val());//金额
            // 如果value不为空
            if (value && value !== '0') {
                len = parseInt(value);
                lenfloat = parseFloat(value);
                if (value.indexOf('-') >= 0) {
                    $('#yqsl').val('');
                } else if ((lenfloat - len) > 0) {
                    $('#yqsl').val(len);
                } else {
                    $('#jz').val(toFixeds(volumeS * len));
                    $('#zl').val(toFixeds(suttleS * len));
                    $('#tj').val(toFixeds(grossWeightS * len));
                    $('#je').val(toFixeds(money * len));

                    // 如果主单位数量为1
                    if (UnitNum === 1) {
                        $('#xdwsl').val(len.toString() + '箱0件');
                    } else {
                        var BOX, PIECE;
                        BOX = parseInt(len / UnitNum);
                        PIECE = len % UnitNum;
                        $('#xdwsl').val(BOX + '箱' + PIECE + '件');
                    }
                }
            } else {
                $('#jz').val(volumeS);
                $('#zl').val(suttleS);
                $('#tj').val(grossWeightS);
                $('#je').val(0);
                $('#xdwsl').val('0箱0件');
            }
        })
        //箱单位离开事件
        $('#xdwsl').blur(function () {
            var value = $('#xdwsl').val();
            var a = value.indexOf('箱');
            var b = value.indexOf('件');
            var c = value.charAt(value.length - 1);
            if (a > 0 && b > 0 && c == '件') {

            } else {
                layer.msg("必须符合 xx箱xx件 的格式!");
            }
        });
        // 监听箱单位数量
        $('#xdwsl').on('input propertychange', function (data) {
                var value, indbox, indpiece, box, piece, boxs, pieces;
                var volumeS = parseFloat($('#jzS').val());//净重
                var suttleS = parseFloat($('#zlS').val());//毛重
                var grossWeightS = parseFloat($('#tjS').val());//体积
                var money = parseFloat($('#qian').val());
                value = data.currentTarget.value;
                indbox = value.indexOf('箱');
                indpiece = value.indexOf('件');
                if (indbox > 0 && indpiece > 0) {
                    box = value.substring(0, indbox);
                    piece = value.substring(indbox + 1, indpiece);
                    var unitNumber = parseInt(box) * UnitNum + parseInt(piece);
                    var len;
                    if (unitNumber !== 0) {
                        len = unitNumber;
                        $('#je').val(toFixeds(money * len));
                    } else {
                        len = 1;
                        $('#je').val(0);
                    }
                    $('#yqsl').val(unitNumber);
                    $('#jz').val(toFixeds(volumeS * len));
                    $('#zl').val(toFixeds(suttleS * len));
                    $('#tj').val(toFixeds(grossWeightS * len));
                }
            }
        )
        /*=========================以下公共方法================================================*/

        //获取当前日期
        function getNowFormatDate() {
            var date = new Date();
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + seperator1 + month + seperator1 + strDate;
            return currentdate;
        }

        function getNowFormatDate1() {
            var date = new Date();
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + month + strDate;
            return currentdate;
        }

        /**
         * 有效期
         * startDate:起始日期
         * valueTime：有效天数
         */
        function transferCouponValueTime(startDate, valueTime) {
            var date = new Date(startDate);
            var newDate = new Date(date.getFullYear(), date.getMonth(), Number(date.getDate()) + Number(valueTime));
            var year1 = date.getFullYear();
            var month1 = date.getMonth() + 1;
            var day1 = date.getDate();
            var year2 = newDate.getFullYear();
            var month2 = newDate.getMonth() + 1;
            var day2 = newDate.getDate();
            /*return "<span>"+year1+"</span>-<span>"+month1+"</span>-<span>"+day1+"</span>一<span>"
                 +year2+"</span>-<span>"+month2+"</span>-<span>"+day2+"</span>";*/

            return year2 + "-" + month2 + "-" + day2;
        }

        function toFixeds(data) {
            var str = "";
            if (data) {
                data = data.toString();
                if (data.indexOf('.') < 0) {
                    str = data;
                } else {
                    str = data.substring(0, data.indexOf('.') + 3);
                }
                return parseFloat(str);
            }
        }

    });

</script>
</html>